﻿using System;
using System.Collections;

using log4net;

namespace Eflong.Utility
{
    public class LogWrapper
    {
        private static readonly Hashtable LoggerCache = Hashtable.Synchronized( new Hashtable( ) );

        public static ILog GetLogger( Type type )
        {
            if ( LoggerCache.ContainsKey( type ) )
            {
                return ( ILog )LoggerCache[ type ];
            }
            ILog nlog = LogManager.GetLogger( type );
            LoggerCache.Add( type , nlog );
            return nlog;
        }

        public static ILog GetLogger<T>( )
        {
            return GetLogger( typeof( T ) );
        }

        public static ILog GetLogger( string name )
        {
            if ( LoggerCache.ContainsKey( name ) )
            {
                return ( ILog )LoggerCache[ name ];
            }
            ILog nlog = LogManager.GetLogger( name );
            LoggerCache.Add( name , nlog );
            return nlog;
        }

        public static ILog Logger
        {
            get
            {
                return LogManager.GetLogger( "BASELOGGER" );
            }
        }

        public static void Info<T>( string message )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsInfoEnabled )
            {
                LogWrapper.Logger.Info( message );
            }
        }

        public static void Info<T>( string message , Exception error )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsInfoEnabled )
            {
                LogWrapper.Logger.Info( message , error );
            }
        }

        public static void InfoFormat<T>( string format , params object[ ] args )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsInfoEnabled )
            {
                LogWrapper.Logger.InfoFormat( format , args );
            }
        }

        public static void Debug<T>( string message )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsDebugEnabled )
            {
                LogWrapper.Logger.Debug( message );
            }
        }

        public static void Debug<T>( string message , Exception error )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsDebugEnabled )
            {
                LogWrapper.Logger.Debug( message , error );
            }
        }

        public static void DebugFormat<T>( string format , params object[ ] args )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsDebugEnabled )
            {
                LogWrapper.Logger.DebugFormat( format , args );
            }
        }

        public static void Error<T>( string message )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsErrorEnabled )
            {
                LogWrapper.Logger.Error( message );
            }
        }

        public static void Error<T>( string message , Exception error )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsErrorEnabled )
            {
                LogWrapper.Logger.Error( message , error );
            }
        }

        public static void ErrorFormat<T>( string format , params object[ ] args )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsErrorEnabled )
            {
                LogWrapper.Logger.ErrorFormat( format , args );
            }
        }

        public static void Fatal<T>( string message )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsFatalEnabled )
            {
                LogWrapper.Logger.Fatal( message );
            }
        }

        public static void Fatal<T>( string message , Exception error )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsFatalEnabled )
            {
                LogWrapper.Logger.Fatal( message , error );
            }
        }

        public static void FatalFormat<T>( string format , params object[ ] args )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsFatalEnabled )
            {
                LogWrapper.Logger.FatalFormat( format , args );
            }
        }

        public static void Warn<T>( string message )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsWarnEnabled )
            {
                LogWrapper.Logger.Warn( message );
            }
        }

        public static void Warn<T>( string message , Exception error )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsWarnEnabled )
            {
                LogWrapper.Logger.Warn( message , error );
            }
        }

        public static void WarnFormat<T>( string format , params object[ ] args )
        {
            ILog logger = GetLogger<T>( );
            if ( LogWrapper.Logger.IsWarnEnabled )
            {
                LogWrapper.Logger.WarnFormat( format , args );
            }
        }
    }
}